package com.yyt.esdemo.controller;

import com.yyt.esdemo.dao.UserDao;
import com.yyt.esdemo.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yeyangtao
 * @date 2020/5/26 10:05
 */
@RestController
public class UserController {
    @Resource
    private UserDao userDao;

    @PostMapping("/addUser")
    public String addUser(String username, String password, Integer age) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(age);
        return String.valueOf(userDao.save(user).getId());
    }

    @DeleteMapping("/deleteUser")
    public String deleteUser(Integer id) {
        userDao.deleteById(id);
        return "Success!";
    }

    @PutMapping("/updateUser")
    public String updateUser(Integer id, String username, String password, Integer age) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(age);
        return String.valueOf(userDao.save(user).getId());
    }

    @GetMapping("/getUser")
    public User getUser(Integer id) {
        return userDao.findById(id).get();
    }

    @GetMapping("/getAllUsers")
    public Iterable<User> getAllUsers() {
        return userDao.findAll();
    }


    @GetMapping("/queryUsers")
    public Iterable<User> queryUsers(String name) {
        return userDao.query(name);
    }
}

